package user.main;

import fileparsing.ReadObservations;
import filewriting.FitnessViewWriter;
import filewriting.ObservationsWriter;
import filewriting.PathDisplayWriter;
import filewriting.ProbabilityMatrixWriter;
import filewriting.PruneGraphWriter;
import filewriting.SimpleViewWriter;
import guiobjects.Observation;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

import user.functions.PruningAlgorithm;

import commonfunctions.ChooseFile;
import commonfunctions.Matrix;
import datacreation.ParsedRealDataCreator;


/**
 * Run this to parse and calculate external data.
 * 
 * @author Trevor Terris
 *
 */
public class RunInputData 
{
	private static int numItems = 1000;
	public static void main (String[] args)
	{
		try 
		{
			System.out.println("Runs through the entire system.");
			System.out.println();
	
	    	System.out.println("What directory should be created for data?");
	    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    	String dirName = br.readLine();
	    	
	    	System.out.println("Creating raw observations.");
			//creates the group of observations
			ParsedRealDataCreator.createData(dirName);
			
			System.out.println("Reading raw observations.");
			
			ArrayList<Observation> initialObs = ReadObservations.listOfObservations(dirName);
			
			ArrayList<Observation> obs = new ArrayList<Observation>();
			for (int i = 0; i < numItems; i++)
				obs.add(initialObs.get(i));

			Matrix weights = new Matrix(numItems);
			Scanner s = ChooseFile.select("Select the file where the probabilities are stored.");
			while (s.hasNext())
			{
				int i = s.nextInt() - 1;
				int j = s.nextInt() - 1;
				double weight = s.nextDouble();
				if (i < numItems && j < numItems && weight > 0.05 && obs.get(j).frameNum - obs.get(i).frameNum == 1)
					weights.set(weight, i, j);
			}
			System.out.println("pruning");
			Matrix tracks = PruningAlgorithm.prune(weights, obs);
			
			TrackerThread threads = new TrackerThread();

			threads.addThread(ObservationsWriter.startWriting(obs, dirName), 
			"Done writing observations to file.");
	
			threads.addThread(ProbabilityMatrixWriter.startWriting(weights, dirName), 
					"Done writing weights to file.");
			
			threads.addThread(PruneGraphWriter.startWriting(tracks, dirName), 
					"Done writing tracks to file.");
			
			threads.addThread(SimpleViewWriter.startWriting(obs, weights, tracks, dirName), 
					"Done writing simplified graphical output.");
		
			threads.addThread(PathDisplayWriter.startWriting(obs, weights, tracks, dirName), 
					"Done writing graphical tracked output.");
			
			threads.addThread(FitnessViewWriter.startWriting(obs, weights, tracks, dirName), 
					"Done writing visualization of path fitness.");
			
			threads.run();
		}
		catch (Exception e)
		{
			System.out.println("IO exception.");
		}
	}
}
